/** Clase para el manejo de la sesion SNMP **/

package common;

import java.io.IOException;

import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;


public class SesionSNMP {

    private String address; //direccion del agente SNMP

    private String community; //comunidad
    
    private Snmp snmp; //objeto para la comunicacion SNMP con el agente

    /* Constructor */
    public SesionSNMP(String address, String community){
        this.address = address;
        this.community = community;
        try {
            start();
        } catch (IOException ex) {
            System.err.println("Error al establecer la sesion SNMP.");
            System.err.println("Exception Message = " + ex.getMessage());
            System.exit(-1);
        }
    }

    /* Establecer los valores del host remoto, comunidad, version, etc */
    public Target getTarget() {
	Address targetAddress = GenericAddress.parse(address); //direccion remota
	CommunityTarget target = new CommunityTarget();
	target.setCommunity(new OctetString(community)); //comunidad
	target.setAddress(targetAddress);
	target.setRetries(2);
	target.setTimeout(2000);
	target.setVersion(SnmpConstants.version2c); //version snmp
	return target;
    }

    /* Establecer el transporte por defecto y ponerse a la escucha */
    public void start() throws IOException {
        TransportMapping transport = new DefaultUdpTransportMapping();
	snmp = new Snmp(transport);
	transport.listen();
    }

    /* Finalizar la sesion SNMP */
    public void stop() throws IOException {
	snmp.close();
    }

    /* Observadores */

    public String getAddress(){
        return this.address;
    }

    public String getCommunity(){
        return this.community;
    }

    public Snmp getSnmp(){
        return this.snmp;
    }

    /* Modificadores*/

    public void setAddress(String address){
        this.address = address;
    }

    public void setCommunity(String community){
        this.community = community;
    }

} //class
